home *** CD-ROM | disk | FTP | other *** search
AMOS Source Code | 1992-09-28 | 7.5 KB | 254 lines |
- '***************************
- '* AMOS Professional * INSTRUCTIONS COVERED
- '* *
- '* PROCEDURES 2 * Global
- '* * Shared
- '* (c) Europress Software * Param
- '* * Param$
- '* Ronnie Simpson * Param#
- '*************************** Pop Proc
- ' On Proc
- ' On Break Proc
- '
- '-------------------------------------------
- 'GENERAL
- '-------------------------------------------
- 'In this the second of the procedure tutorials we will explore the ways
- 'in which information can be passed between procedures and the rest of
- 'the program.
- '-------------------------------------------
- 'GLOBAL AND SHARED VARIABLES
- '-------------------------------------------
- 'Global variables are declared from the main program before the procedure
- 'is called. (this only has to be done once)
- '
- 'eg. Global A,B,C$,D()
- '
- 'When a variable or list of variables is declared global then they can be
- 'accessed from anywhere in the program. (both inside and outside procedures)
- 'This provides an easy method of transferring the information between any
- 'number of procedures
- '
- 'A second method of passing information is to declare the variable shared.
- '
- 'eg. Shared A,B,C$,D()
- '
- 'This instruction is placed inside the procedure and the first time the
- 'procedure is called then the listed variables will be treated as global.
- 'Any arrays to be declared in this way must first have been dimensioned
- 'in the main part of the program.
- '-------------------------------------------
- 'PASSING PARAMETERS
- '-------------------------------------------
- 'When calling a procedure it is possible to pass parameters.
- '
- 'eg. Proc MULTIPLY[A,B]
- '
- 'The parameters are placed inside square brackets after the procedure name
- 'and these should be matched with a similar set in the procedure definitions.
- '
- 'eg. A=100 : B=$"AMOS "
- ' Proc MULTIPLY[A,B$]
- ' : :
- ' Procedure MULTIPLY[X,Y$]
- ' Print X*2
- ' Print Y$+Y$
- ' End Proc
- '
- 'A couple of things to note from the above example is that:-
- ' (1)The number and type of parameters must match with those in the
- ' procedure definition.
- ' (2)The variables used in the procedure definition can be
- ' local to the procedure.
- '
- 'It is also possible to return a variable or string from the procedure
- 'to the main program using a similar method as above but placing the
- 'square brackets after the End Proc
- '
- 'eg. A=100 : B$="AMOS "
- ' Proc MULTIPLY[A,B$]
- ' Print Param
- ' : :
- ' Procedure MULTIPLY[X,Y$]
- ' X=X*2
- ' Print Y$+Y$
- ' End Proc[X]
- '
- 'Please note that only one parameter can be returned in this way and
- 'depending on the type of variable it is the result will be held in
- 'one of the following functions:-
- '
- '
- ' Param (an integer)
- ' Param$ (a string)
- ' Param# (a real number or floating point)
- '
- '------------------------------------------- '
- 'POP PROC
- '-------------------------------------------
- 'Normally once a procedure has been called, control will only be returned to
- 'the main program when the End Proc is reached, but if a hasty return is
- 'required the Pop Proc instruction will immediately abort the procedure and
- 'return control to the instruction following the call.
- '
- 'eg. Proc QUIT
- ' Edit
- '
- ' Procedure QUIT
- ' For X=1 To 5000
- ' If X=1000 Then Pop Proc
- ' Next
- ' Print "I'll never get printed"
- ' End Proc
- '
- '-------------------------------------------
- 'On Break Proc
- '-------------------------------------------
- 'A jump can also be made to a procedure whenever the program is interupted
- 'by using the On Break Proc instruction:-
- '
- ' On Break Proc GOODBYE
- ' Do
- ' Loop
- ' Procedure GOODBYE
- ' Print "bye for now" : Wait 100 : Edit
- ' End Proc
- '
- '-------------------------------------------
- 'WORKING EXAMPLE
- '-------------------------------------------
- 'I am sure with a little effort you will be able to use your new found
- 'programming skills to turn this program into a rival of D/Paint IV.
- '-------------------------------------------
- '
- Dim M$(5)
- Global M$(),M
- 'Rem *** open hires screen and set palette
- '
- Screen Open 0,640,200,16,Hires : Flash Off : Change Mouse 2
- Palette $0,$F00,$F0,$F,$FF0,$F0F,$FF,$F70,$7F,$70F,$F07,$333,$666,$999,$CCC,$FFF
- Curs Off : Cls 0 : Paper 11 : Pen 14
- '
- '
- Rem *** reserve some memory for the screen zones
- '
- Reserve Zone 21
- '
- '
- Rem *** make the variable C global (procedures use this as the ink colour)
- Global C
- '
- '
- Rem *** start the main program by calling the INIT procedure
- '
- INIT
- '
- '
- Do
- Clip
- AWAIT
- Exit If Param=5
- If Param>5
- Proc CHANGECOLOUR[Param-6]
- Else On M Proc PHILL,LINE,SIRCLE,RECTANGLE
- End If
- Loop
- '
- '
- Edit
- '
- '
- '
- Rem *** the various procedures
- '
- Procedure INIT
- M$(1)="Fill "
- M$(2)="Line "
- M$(3)="Circle "
- M$(4)="Rectangle"
- M$(5)="Quit "
- M=3
- Locate 73,1 : Print Border$(Zone$(" FILL ",1),2)
- Locate 73,4 : Print Border$(Zone$(" LINE ",2),2)
- Locate 73,7 : Print Border$(Zone$("CIRCLE",3),2)
- Locate 73,10 : Print Border$(Zone$(" RECT.",4),2)
- Locate 73,13 : Print Border$(Zone$(" QUIT ",5),2)
- X=590 : Y=128 : Set Paint 1
- For I=0 To 7
- Ink I,,8
- Bar X,Y To X+16,Y+8 : Set Zone I+6,X,Y To X+16,Y+8
- Add Y,9
- Next
- X=610 : Y=128
- For I=8 To 15
- Ink I,,3
- Bar X,Y To X+16,Y+8 : Set Zone I+6,X,Y To X+16,Y+8
- Add Y,9
- Next
- Ink 1,,15 : Bar 576,121 To 639,126
- Ink 15 : Box 0,0 To 570,188
- Ink 11,,14
- Bar 0,190 To 570,200
- C=1
- End Proc
- Procedure AWAIT
- Pen 14 : Paper 11
- Locate 1,24 : Print " Current Mode-";M$(M);
- Wait 10
- Do
- X=Mouse Zone
- If X>0 and X<5 and Mouse Key Then M=X : Locate 1,24 : Print " Current Mode-";M$(M);
- Exit If X=0 and Mouse Key
- Exit If X>4 and Mouse Key
- Loop
- End Proc[X]
- Procedure CHANGECOLOUR[X]
- C=X
- Pen C : Locate 60,24 : Print "COLOUR>>>>";
- Ink C,,15 : Bar 576,121 To 639,126
- Wait 20
- End Proc
- Procedure LINE
- Shared X,Y
- Gr Writing 2
- POSITION
- While Mouse Key
- X2=X Screen(X Mouse) : Y2=Y Screen(Y Mouse)
- Draw X,Y To X2,Y2 : Draw X,Y To X2,Y2
- Wend
- Gr Writing 0 : Ink C : Draw X,Y To X2,Y2
- End Proc
- Procedure SIRCLE
- Shared X,Y
- Gr Writing 2
- POSITION
- While Mouse Key
- X2=X Screen(X Mouse) : Y2=Y Screen(Y Mouse)
- R1=Abs(X-X2)/2 : R2=Abs(Y-Y2) : R=Max(R1,R2) : If R<1 Then R=1
- Circle X,Y,R : Circle X,Y,R
- Wend
- Gr Writing 0 : Ink C : Circle X,Y,R
- End Proc
- Procedure RECTANGLE
- Shared X,Y
- Gr Writing 2
- POSITION
- While Mouse Key
- X2=X Screen(X Mouse) : Y2=Y Screen(Y Mouse)
- Box X,Y To X2,Y2 : Box X,Y To X2,Y2
- Wend
- Gr Writing 0 : Ink C : Box X,Y To X2,Y2
- End Proc
- Procedure PHILL
- Repeat
- X=X Screen(X Mouse) : Y=Y Screen(Y Mouse)
- Until X>0 and X<570 and Y>0 and Y<188 and Mouse Key
- Ink C : Paint X,Y
- End Proc
- Procedure POSITION
- Shared X,Y
- Repeat
- X=X Screen(X Mouse) : Y=Y Screen(Y Mouse)
- Until Mouse Key
- Clip 1,1 To 569,187
- End Proc